package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.YofotoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author syh
 * @Date 2018/9/13
 */
@RestController
@RequestMapping("/yofoto")
@Api(value = "/yofoto", description = "WMS与报单接口 _hgl")
public class YofotoController extends BaseController<YofotoService,WmsShipmentOrder>{
    //<!------------------------被动提供接口------------------------>
    /**
     * 出库订单状态查询接口
     * */
    @ApiOperation(value = "出库订单状态查询",response = JSONObject.class, notes = "出库订单状态查询接口")
    @PostMapping(value="/list")
    public  @ResponseBody JSONObject getselect(@RequestBody Map<String, String> map) {
        JSONObject jsonObject= new JSONObject();
        WmsShipmentOrder wmsShipmentOrder=baseService.getselect(map.get("sono"), map.get("works"));
        Integer count=baseService.selectReview();
        jsonObject.put("sono",wmsShipmentOrder.getFybh());
        jsonObject.put("whouse",wmsShipmentOrder.getCkid());
        jsonObject.put("works",wmsShipmentOrder.getSshzid());
        if("0".equals(wmsShipmentOrder.getBcid())){
            jsonObject.put("sostatus","1");
            jsonObject.put("description","未下发");
        }
        if(!"0".equals(wmsShipmentOrder.getBcid()) || "2".equals(wmsShipmentOrder.getJhzt())||count>0){
            jsonObject.put("sostatus","2");
            jsonObject.put("description","发货中");
        }
        if("3".equals(wmsShipmentOrder.getFyzt())|| "4".equals(wmsShipmentOrder.getFyzt())){
            jsonObject.put("sostatus","3");
            jsonObject.put("description","已发货");
        }
        if(wmsShipmentOrder==null){
            jsonObject.put("sostatus","4");
            jsonObject.put("description","出库单不存在");
        }
        return jsonObject;
    }

    /**
     * 出库订单修改接口
     * */
    @ApiOperation(value = "出库订单修改",response = JSONObject.class, notes = "出库订单修改接口")
    @PostMapping(value="/update")
    public  @ResponseBody JSONObject getUpdate(@RequestBody Map<String,Object> map) {
        JSONObject jsonObject= new JSONObject();
        Map<String,Object>  hashmap=(Map)map.get("head");
        WmsShipmentOrder wmsShipmentOrder=new WmsShipmentOrder();
        wmsShipmentOrder.setFybh(hashmap.get("sono").toString());
        wmsShipmentOrder.setCkid(hashmap.get("whouse").toString());
        wmsShipmentOrder.setSshzid(hashmap.get("works").toString());
        wmsShipmentOrder.setKhdz(hashmap.get("address").toString());
        wmsShipmentOrder.setLxrxm(hashmap.get("contact").toString());
        wmsShipmentOrder.setLxrdh(hashmap.get("tel").toString());
        if(!"".equals(wmsShipmentOrder.getFybh())){
            WmsShipmentOrder wmsShipmentOrder1=baseService.selectFy(wmsShipmentOrder);
            if("0".equals(wmsShipmentOrder1.getFyzt())||"1".equals(wmsShipmentOrder1.getFyzt())||"2".equals(wmsShipmentOrder1.getFyzt())){
                Integer update=baseService.getUpdate(wmsShipmentOrder);
                if(update>0){
                    jsonObject.put("backcode",0);
                    jsonObject.put("backmsg","出库订单修改成功");
                }else{
                    jsonObject.put("backcode",-1);
                    jsonObject.put("backmsg","出库订单修改失败");
                }
            }else{
                jsonObject.put("backmsg","已发运不可进行修改");
            }
        }else{
            jsonObject.put("backmsg","发运单号为空修改失败");
        }


        return jsonObject;
    }

    /**
     * 销售出库单取消
     * */
    @ApiOperation(value = "销售出库单取消",response = JSONObject.class, notes = "销售出库单取消接口")
    @PostMapping(value="/updateZt")
    public  @ResponseBody JSONObject getDelete(@RequestBody Map<String,Object> map) {
        JSONObject jsonObject= new JSONObject();
        try {
            Integer delete=baseService.getDelete(map.get("sono").toString());
            if(delete>0){
                jsonObject.put("backcode",0);
                jsonObject.put("backmsg","销售出库单取消成功");
            }else{
                jsonObject.put("backcode",-1);
                jsonObject.put("backmsg","销售出库单取消失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    /**
     *销售出库单下发接口
     * */
    @PostMapping(value = "/getIssueOrder")
    @ApiOperation(value = "销售出库单下发接口 新增发运订单和明细",response = JSONObject.class, notes = "POST新增发运订单和明细")
    public @ResponseBody JSONObject addReturnOrder(@RequestBody String requestBody, HttpSession session) {
        JSONObject jsonObject = JSONObject.fromObject(requestBody);
        JSONObject json=new JSONObject();
        //Map<String,Object> map = new HashMap<>();
        Map<String,Object> maps=(Map<String,Object>)jsonObject.get("head");
        try{
            WmsShipmentOrder Order=baseService.selectSfRepetition(String.valueOf(maps.get("sono")));
            if(Order!=null){
                json.put("backcode",-1);
                json.put("backmsg",String.valueOf(maps.get("sono"))+"发运单号已存在");
            }else{
                /*UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
                String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
                List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
                if(hz.size()>0) {
                    String hzid=hz.get(0).getHzid();*/
                jsonObject.put("hzid",ckfhInfo.quartz_wms_owner_id.getType());
                /*}*/
                jsonObject.put("ckid",ckfhInfo.quartz_ckid.getType());
                baseService.addFy(jsonObject);
            }
        } catch (SucessException e) {
            json.put("backcode",0);
            json.put("backmsg","订单添加成功");
        }catch (RuntimeException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            json.put("backcode",-1);
            json.put("backmsg","订单添加失败");
        }catch (Exception e) {
            e.printStackTrace();
            json.put("backcode",-1);
            json.put("backmsg","订单添加失败");
        }
        return json;
    }
    //<!------------------------主动调用接口------------------------>
    /**
     * 获取传入参数和路径
     * @param wmsShipmentOrder
     * @param ckid
     * @param sshzid
     * @param posturl
     * @return
     * @throws Exception
     */
    private JSONObject getJsonObject(@RequestBody WmsShipmentOrder wmsShipmentOrder, String ckid, String sshzid, String posturl) throws Exception {
        JSONObject json=null;
        json=new JSONObject();
        json.put("works","S.01");
        json.put("whouse","SB01");
        json.put("sono",wmsShipmentOrder.getSono());
        json.put("sotype",wmsShipmentOrder.getSotype());
        /*json.put("sono","REO2017030324");
        json.put("sotype","104");*/
        /*json.put("works",sshzid);
        json.put("whouse",ckid);
        */
        json= InterfaceCall.allocation(json,posturl);
        return json;
    }
    /**
     * 领料出库单下发查询接口
     * */
    @PostMapping(value = "/getSeachFydd")
    @ApiOperation(value = "查询领料出库单下发接口",response = JSONObject.class, notes = "查询领料出库单下发接口")
    public @ResponseBody JSONObject  getSeachFydd (@RequestBody WmsShipmentOrder wmsShipmentOrder, HttpSession session) throws Exception{
        JSONObject jsonObject =new JSONObject();
        List list=new ArrayList();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String sshzid=null;
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            String posturl="/easApi/getMaterialReqOrder";
            JSONObject json = getJsonObject(wmsShipmentOrder, ckid, sshzid, posturl);
            wmsShipmentOrder= baseService.getSeachFydd(json,wmsShipmentOrder);

            list.add(wmsShipmentOrder);
            jsonObject=  SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    , list.size());
        }catch (ServiceException e){
            jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"领料出库单下发接口查询失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            jsonObject = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"领料出库单下发接口查询失败","领料出库单下发接口查询失败","");
        }
        return jsonObject;
    }

    @PostMapping(value = "/getInsertDBPoOrder")
    @ApiOperation(value = "新增领料出库单下发接口",response = JSONObject.class, notes = "新增领料出库单下发接口")
    public JSONObject  getInsertDBPoOrder (@RequestBody WmsShipmentOrder wmsShipmentOrder, HttpSession session){
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String sshzid = null;
            if (hz.size() > 0) {
                sshzid = hz.get(0).getHzid();
            }
            wmsShipmentOrder.setCjr(userinfo.getUser().getName());
            //添加前去重
            WmsShipmentOrder wmsShipmentOrder1 = baseService.selectSfRepetition(wmsShipmentOrder.getSono());
            if (wmsShipmentOrder1 != null) {
                json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.warning.toString(), "发运订单添加失败", "发运订单已存在", "");
            } else {
                String posturl="/easApi/getMaterialReqOrder";
                JSONObject jsonObject = getJsonObject(wmsShipmentOrder, ckid, sshzid, posturl);
                baseService.Add(jsonObject,wmsShipmentOrder);
            }
        }catch (SucessException e){
            json =  SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"发运订单添加成功","", "");;
        }catch (RuntimeException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"发运订单添加失败",e.getMessage(),"");
        }catch (ServiceException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"发运订单添加失败",e.getMessage(),"");
        }catch (Exception e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"发运订单添加失败","发运订单添加失败","");
        }
        return json;
    }
    @PostMapping(value = "/getDdlx")
    @ApiOperation(value = "订单类型查询信息",response = JSONObject.class, notes = "订单类型查询信息")
    public @ResponseBody JSONObject getDdlx(HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        List<WmsOwnerOrdertype> list =new ArrayList();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            String sshzid = null;
            if (hz.size() > 0) {
                sshzid = hz.get(0).getHzid();
            }
            list = baseService.getDdlx(ckid,sshzid);
            jsonObject = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}
